package com.loiane.main;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.loiane.com.model.City;
import com.loiane.dao.CityDAO;
import com.loiane.util.HibernateUtil;

/**
 * To execute this project, you should run this class
 * 
 * @author Loiane Groner
 * http://loianegroner.com (English)
 * http://loiane.com (Portuguese)
 */
public class Main {
public static void main(String[] args) throws IOException {
	
	CityDAO cityDAO = new CityDAO();
	cityDAO.setSessionFactory(HibernateUtil.getSessionFactory());
	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	Transaction transaction = session.beginTransaction();
	
		
	
		
		
		
			
		
		City cyty1 = new City("xyz1");
		City cyty2 = new City("xyz2");
		City cyty3 = new City("xyz3");
		City cyty4 = new City("xyz4");
		City cyty5 = new City("xyz5");
		//cyty5.setId(99);
		

		cityDAO.save(cyty4);
		cityDAO.save(cyty1);
		cyty1.setName("updated");
        List<City> c=new LinkedList<City>();
		c.add(cyty2);
        for(int i=0 ;i<10;i++){c.add(new City("xyz_"+i));}
        c.add(cyty4);
        cyty1.setChilds(c);
        cityDAO.flush();
        //System.in.read();
        //cityDAO.remove(cyty1);
        
		System.out.println(cyty1.getId());
		long id=cyty1.getId();
		transaction.commit();
		
		cityDAO = new CityDAO();
		cityDAO.setSessionFactory(HibernateUtil.getSessionFactory());
		 session = HibernateUtil.getSessionFactory().getCurrentSession();
		transaction = session.beginTransaction();
		System.out.println("zzzzzzz:"+id);
		cyty1=cityDAO.find(id);
		//System.out.println(cyty1.getParent());
		//System.out.println(cyty1.getChilds());
		
	}
}
